//
//  PropertyVC.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/16.
//  Copyright © 2017年 毕小强. All rights reserved.
//
//  属性
import UIKit

class PropertyVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        /*
         *      存储属性只能有类和结构体定义
         *      计算属性可以由结构体,类,枚举定义
         **/
        
        ///存储属性
        var rangeOfThreeItems = FixedLengthRange(firstValue: 100, length: 20)
        rangeOfThreeItems.firstValue = 200
        
        
        /// 延迟加载
        class DataImpoter{
            var fileName = "data.txt"
        }
        
        class DataManager{
            lazy var importer = DataImpoter()
            var data = [String]()
        }
        
        let manager = DataManager()
        manager.data.append("Some data")
        manager.data.append("Some more data")
        print(manager.importer.fileName)
        
        ///计算属性
        struct Point{
            var x = 0.0,y = 0.0
        }
        struct Size{
            var width = 0.0,height = 0.0
        }
        struct Rect{
            var origin = Point()
            var size = Size()
            var center :Point {
                get{
                    let centerX = origin.x+(size.width/2)
                    let centerY = origin.y+(size.height/2)
                    return Point(x: centerX, y: centerY)
                }
                set{
                    origin.x = newValue.x - (size.width / 2)
                    origin.y = newValue.y - (size.height / 2)
                }
            }
        }
        var square = Rect(origin:Point(x:0.0,y:0.0),size:Size(width:10.0,height:10.0))
        let squareCenter = square.center
        square.center = Point(x:10.0,y:10.0)
        print("\(squareCenter.x) \(square.center.x)")
        
        ///只读属性
        struct Cuboid{
            var width = 0.0,height = 0.0,depth = 0.0
            var volume:Double {
                return width*height*depth
            }
        }
        
        ///属性观察者
        class StepCounter {
            var totalSteps: Int = 0 {
                willSet{
                    print("New:\(newValue)")
                }
                
                didSet{
                    if totalSteps > oldValue {
                        print("Add \(totalSteps - oldValue) steps")
                    }
                }
            }
        }
        let stepCounter = StepCounter()
        stepCounter.totalSteps = 200
        stepCounter.totalSteps = 300
        stepCounter.totalSteps = 400
        
        ///类型属性语法
        struct SomeStructure{
            var width = 0.0
            
            static var storedTypeProperty = "Some value"
            static var computedTypeProperty:Int{
                return 1
            }
        }
        
        enum SomeEnumeration{
            static var storedTypeProperty = "Some value"
            static var computedTypeProperty:Int{
                return 6
            }
        }
        
        class SomeClass{
            static var storedTypeProperty = "Some value"
            static var computedTypeProperty:Int{
                return 27
            }
            class var overrideableComputedTypeProperty:Int {
                return 107
            }
        }
        
        print(SomeStructure.storedTypeProperty)
        SomeStructure.storedTypeProperty = "Another value"
        print(SomeStructure.storedTypeProperty)
        print(SomeEnumeration.computedTypeProperty)
        print(SomeClass.overrideableComputedTypeProperty)
        
        struct AudioChannel{
            static let maxLevel = 10
            static var inputLevel = 0
            var currentLevel:Int = 0{
                didSet{
                    if currentLevel > AudioChannel.maxLevel {
                        currentLevel = AudioChannel.maxLevel
                    }
                    
                    if currentLevel > AudioChannel.inputLevel {
                        AudioChannel.inputLevel = currentLevel
                    }
                }
            }
            
        }
        var leftChannel = AudioChannel()
        var rightChannel = AudioChannel()
        leftChannel.currentLevel = 7
        print(AudioChannel.inputLevel)
        rightChannel.currentLevel = 11
        print(AudioChannel.inputLevel)
        
        // Do any additional setup after loading the view.
    }

    struct FixedLengthRange{
        var firstValue : Int
        let length : Int
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
